package com.lkd.controller;

import com.lkd.dto.PartnerDto;
import com.lkd.entity.Partner;
import com.lkd.service.IPartnerService;
import com.lkd.vo.PageBean;
import com.lkd.vo.PartnerInfoVo;
import com.lkd.vo.PartnerVo;
import com.lkd.vo.Result;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;

/**
 * ClassName: PartnerController
 * Package: com.lkd.controller
 * Description:
 *
 * @Author zt
 * @Create 2025/10/10 18:37
 * @Version 1.0
 */
@RequestMapping("/api/user-service/partner")
@RestController
@RequiredArgsConstructor
@Slf4j
public class PartnerController {

    private final IPartnerService partnerService;
    /**
    新增合作商
     */
    @PostMapping
    public Result save(@RequestBody Partner partner){
        partner.setCreateTime(LocalDateTime.now());
        partner.setUpdateTime(LocalDateTime.now());
        partnerService.save(partner);
        return Result.success();
    }

    /**
     * 合作商搜索
     */
    @GetMapping("/search")
    public Result search(PartnerDto partnerDto){
        PageBean<PartnerVo> pageBean = partnerService.search(partnerDto);
        return Result.success(pageBean);
    }

    /**
     * 获取合作商详情
     */
    @GetMapping("/{id}")
    public Result getPartnerInfo(@PathVariable Integer id){
        PartnerInfoVo partnerInfoVo =partnerService.getPartnerInfo(id);
        return Result.success(partnerInfoVo);
    }

    /**
     * 修改合作商
     */
    @PutMapping
    public Result modifyPartner(@RequestBody Partner partner){
        //填补更新时间
        partner.setUpdateTime(LocalDateTime.now());
        partnerService.updateById(partner);
        return Result.success();
    }

    /**
     * 删除合作商
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable Integer id){
        partnerService.removeById(id);
        return Result.success();
    }

    /**
     * 统计合作商的数量
     */
    @GetMapping("/count")
    public Result partnerCount(){
        Long count = partnerService.lambdaQuery().count();
        log.info("合作商的数量：{}", count);
        return Result.success(count);
    }

}
